package com.ruoyi.ipms_crm.service.impl;

import java.util.List;

import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.utils.IPMSUtils;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.ipms_crm.dto.ContractDetailsDto;
import com.ruoyi.ipms_crm.entity.IpmsContract;
import com.ruoyi.ipms_crm.mapper.IpmsContractMapper;
import com.ruoyi.ipms_crm.service.IpmsContractService;
import com.ruoyi.system.api.entity.Inventor;
import com.ruoyi.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 合同Service业务层处理 @iCAN
 */
@Service
public class IpmsContractServiceImpl implements IpmsContractService
{
    @Autowired
    private IpmsContractMapper ipmsContractMapper;

    /**
     * 查询合同
     *
     * @param contractId 合同主键
     * @return 合同
     */
    @Override
    public IpmsContract selectIpmsContractByContractId(Integer contractId)
    {
        return ipmsContractMapper.selectIpmsContractByContractId(contractId);
    }

    /**
     * 查询合同列表
     *
     * @param ipmsContract 合同
     * @return 合同
     */
    @Override
    public List<IpmsContract> selectIpmsContractList(IpmsContract ipmsContract)
    {
        return ipmsContractMapper.selectIpmsContractList(ipmsContract);
    }

    /**
     * 新增合同
     *
     * @param ipmsContract 合同
     * @return 结果
     */
    @Override
    public int insertIpmsContract(IpmsContract ipmsContract)
    {
        LoginUser loginUser= SecurityUtils.getLoginUser();

        ipmsContract.setCreateBy(loginUser.getSysUser().getNickName());
        ipmsContract.setCreateTime(DateUtils.getNowDate());
        ipmsContract.setVersion(Constants.VERSION_LOCK);
        ipmsContract.setIsDelete(Constants.IS_DELETED);
        ipmsContract.setUpdateBy(loginUser.getSysUser().getNickName());
        ipmsContract.setUpdateTime(DateUtils.getNowDate());
        int empId = Integer.parseInt(loginUser.getUserid()+"");
        ipmsContract.setEmpId(empId);
        //合同编号
        ipmsContract.setContractNo(IPMSUtils.spawnNo("CON.", 5));
        return ipmsContractMapper.insertIpmsContract(ipmsContract);
    }

    /**
     * 修改合同
     *
     * @param ipmsContract 合同
     * @return 结果
     */
    @Override
    public int updateIpmsContract(IpmsContract ipmsContract)
    {
        ipmsContract.setUpdateTime(DateUtils.getNowDate());
        return ipmsContractMapper.updateIpmsContract(ipmsContract);
    }

    /**
     * 批量删除合同
     *
     * @param contractIds 需要删除的合同主键
     * @return 结果
     */
    @Override
    public int deleteIpmsContractByContractIds(Integer[] contractIds)
    {
        return ipmsContractMapper.deleteIpmsContractByContractIds(contractIds);
    }

    /**
     * 删除合同信息
     *
     * @param contractId 合同主键
     * @return 结果
     */
    @Override
    public int deleteIpmsContractByContractId(Integer contractId)
    {
        return ipmsContractMapper.deleteIpmsContractByContractId(contractId);
    }

    @Override
    public ContractDetailsDto getDetails(Integer contractId) {
        return ipmsContractMapper.selectDetails(contractId);
    }

    /* 案件编号找合同费用 */
    @Override
    public List<IpmsContract> selectContractByCaseNo(String caseNo) {
        return ipmsContractMapper.selectContractByCaseNo(caseNo);
    }
}
